package com.github.xuemingyu.springboot.jkfaststart.controller.admin;

import com.bidanet.bdcms.core.bean.AjaxCallBack;
import com.bidanet.bdcms.core.vo.Page;
import com.bidanet.springmvc.demo.jkbuilder.JkFormBuilder;
import com.bidanet.springmvc.demo.jkbuilder.JkTableBuilder;
import com.github.xuemingyu.springboot.jkfaststart.JkConfig;
import com.github.xuemingyu.springboot.jkfaststart.common.AjaxPage;
import com.github.xuemingyu.springboot.jkfaststart.controller.admin.view.role.AddRoleView;
import com.github.xuemingyu.springboot.jkfaststart.controller.admin.view.role.EditPermission;
import com.github.xuemingyu.springboot.jkfaststart.controller.admin.view.role.RoleHeader;
import com.github.xuemingyu.springboot.jkfaststart.controller.admin.view.role.RoleTable;
import com.github.xuemingyu.springboot.jkfaststart.domain.AdminPermission;
import com.github.xuemingyu.springboot.jkfaststart.domain.AdminRole;
import com.github.xuemingyu.springboot.jkfaststart.service.AdminPermissionService;
import com.github.xuemingyu.springboot.jkfaststart.service.AdminRoleService;
import com.google.common.base.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(JkConfig.ADMIN_PATH+"/role")
public class RoleAdminController   {
    @Autowired
    AdminRoleService adminRoleService;
    @Autowired
    AdminPermissionService adminPermissionService;

    @RequestMapping("/index")
    public String index(Model model){

        return JkTableBuilder.create(RoleTable.class)
                .addHeaderTool(new RoleHeader()).build(model);
    }

    @RequestMapping("/data")
    @ResponseBody
    public AjaxPage data(AdminRole adminRole, Page<AdminRole> page){
        adminRole.setDelete(false);
        adminRoleService.eqPage(adminRole,page);
        return AjaxPage.success(page);

    }
    @RequestMapping("/add")
    public String add(AddRoleView addRoleView, Model model){
        return JkFormBuilder.create(addRoleView).build(model);
    }

    @RequestMapping("/save")
    @ResponseBody
    public AjaxCallBack addSave(AddRoleView addRoleView){
        AdminRole adminRole=new AdminRole();
        if(Objects.isNull(addRoleView.getId())){
            adminRole.setDelete(false);
        }else {
            adminRole=adminRoleService.get(addRoleView.getId());
        }
        BeanUtils.copyProperties(addRoleView,adminRole);
        adminRoleService.save(adminRole);
        return AjaxCallBack.saveSuccess();
    }

    @RequestMapping("/edit")
    public String edit(Long id, Model model){
        AdminRole adminRole = adminRoleService.get(id);
        AddRoleView addRoleView = new AddRoleView();
        BeanUtils.copyProperties(adminRole,addRoleView);
        return JkFormBuilder.create(addRoleView).build(model);
    }


    @RequestMapping("/delete")
    @ResponseBody
    public AjaxCallBack delete(Long id ){
        AdminRole adminRole = adminRoleService.get(id);
        adminRole.setDelete(true);
        adminRoleService.save(adminRole);
        return AjaxCallBack.deleteSuccess();
    }

    @RequestMapping("/editPermission")
    public String EditPermission(Model model, EditPermission editPermission){
        AdminRole adminRole = adminRoleService.get(editPermission.getId());
        BeanUtils.copyProperties(adminRole,editPermission);
        List<String>list=new ArrayList<>();
        for(AdminPermission adminPermission :adminRole.getPermissionSet()){
            list.add(adminPermission.getId().toString());
        }
        editPermission.setPermissionSet(list);
        return JkFormBuilder.create(editPermission).build(model);
    }


    @RequestMapping("/updatePermission")
    @ResponseBody
    public AjaxCallBack updatePermission(EditPermission editPermission){
        AdminRole adminRole = adminRoleService.get(editPermission.getId());
        Map<String,AdminPermission>map=new HashMap<>();
        Set<AdminPermission>list=new HashSet<>();
        for(String id:editPermission.getPermissionSet()){
            AdminPermission adminPermission=adminPermissionService.get(Long.parseLong(id));
            insertMap(map,id,list);
            AdminPermission adminPermissionParent = adminPermissionService.get(adminPermission.getPid());
            if(!Objects.isNull(adminPermissionParent)) {
                System.out.println(adminPermissionParent.getId());
                insertMap(map, adminPermissionParent.getId().toString(),list);
            }
            if(!Objects.isNull(adminPermissionParent.getPid())) {
                insertMap(map, adminPermissionParent.getPid().toString(),list);
            }
        }
        adminRole.setPermissionSet(list);
        adminRoleService.save(adminRole);
        return AjaxCallBack.saveSuccess();
    }

    private void  insertMap(Map<String,AdminPermission>map,String id,Set<AdminPermission>list){
        AdminPermission adminPermission=adminPermissionService.get(Long.parseLong(id));
        if(Objects.isNull(map.get(id))&&(!Strings.isNullOrEmpty(id))) {
            map.put(id, adminPermission);
            list.add(adminPermission);
        }
    }
}
